package loginTest;

import jdbc.MyJDBCUtil;
import loginTest.dao.impl.UserDaoimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

@WebServlet("/c")
public class AddCookie extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserDaoimpl user=new UserDaoimpl();
        try {
            User user1 = user.getUser();
            String name=req.getParameter("username");
            String pass=req.getParameter("password");


            if ( user1.getName().equals(name)
                 &&user1.getPassword().equals(pass)){
                Cookie ck=new Cookie("username",name);
                ck.setMaxAge(3*24*3600);
                resp.addCookie(ck);
                req.getRequestDispatcher("loginok").forward(req,resp);
            }else if(user1.getName().equals(name)==false){
                Cookie ck1=new Cookie("name","error");
                ck1.setMaxAge(1);
                resp.addCookie(ck1);
            }else if (user1.getPassword().equals(pass)==false){
                Cookie ck2=new Cookie("pass","error");
                ck2.setMaxAge(1);
                resp.addCookie(ck2);
            }
            resp.sendRedirect("/login/show");

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
